package com.power.leopard.remote.common.codec.java;

import java.io.InputStream;
import java.io.OutputStream;

import com.power.leopard.common.exception.SerializationException;
import com.power.leopard.remote.common.codec.DefaultAbstractSerializer;

public class JavaSerializer extends DefaultAbstractSerializer {

	private static ClassLoader classLoader = JavaSerializer.class.getClassLoader();

	public Object deserializeRequest(InputStream is) throws SerializationException {
		try {
			CompactObjectInputStream iss = new CompactObjectInputStream(is, classLoader);
			try {
				return iss.readObject();
			} finally {
				iss.close();
			}
		} catch (Throwable t) {
			throw new SerializationException(t);
		}
	}

	public void serializeRequest(OutputStream os, Object obj) throws SerializationException {
		try {
			CompactObjectOutputStream oss = new CompactObjectOutputStream(os);
			try {
				oss.writeObject(obj);
				oss.flush();
			} finally {
				oss.close();
			}
		} catch (Throwable t) {
			throw new SerializationException(t);
		}
	}

	public Object deserializeResponse(InputStream is) throws SerializationException {
		return deserializeRequest(is);
	}

	public void serializeResponse(OutputStream os, Object obj) throws SerializationException {
		serializeRequest(os, obj);
	}

}
